package com.netease.financial.ui.activity;

import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.netease.financial.NfsApplication;
import com.netease.financial.R;
import com.netease.financial.base.lockpattern.LockPatternHelper;
import com.netease.financial.base.push.PushHelper;
import com.netease.financial.base.suspendwindow.ChatBallUtil;
import com.netease.financial.internal.di.components.ApplicationComponent;
import com.netease.financial.internal.di.modules.ActivityModule;
import com.netease.financial.navigation.Navigator;

import butterknife.ButterKnife;

public abstract class BaseActivity extends Activity {

    private static final String TAG = BaseActivity.class.getSimpleName();

    private TextView mLeftNavigationItem;
    private ImageButton mRightNavigationItem;
    private TextView mTitleTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        this.getApplicationComponent().inject(this);
        handleChatBall();
        handleLockPattern();
        initActionBar();
    }

    @Override
    protected void onResume() {
        super.onResume();
        PushHelper.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        PushHelper.onPause(this);
    }

    private void initActionBar() {
        ActionBar actionBar = getActionBar();
        if (actionBar == null) {
            return;
        }

        actionBar.setCustomView(R.layout.action_bar_custom_view);

        View customView = actionBar.getCustomView();
        mLeftNavigationItem = ButterKnife.findById(customView, R.id.tv_nav_back);

        mTitleTextView = ButterKnife.findById(customView, R.id.tv_title);

        mRightNavigationItem = ButterKnife.findById(customView, R.id.btn_right);

        actionBar.setDisplayHomeAsUpEnabled(false);
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);
    }

    protected TextView getLeftNavigationItem() {
        return mLeftNavigationItem;
    }

    protected void enableLeftNavigationItem(boolean enable) {
        if (mLeftNavigationItem == null) {
            return;
        }
        if (enable) {
            mLeftNavigationItem.setVisibility(View.VISIBLE);
            mLeftNavigationItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        } else {
            mLeftNavigationItem.setVisibility(View.INVISIBLE);
        }
    }

    protected ImageButton getRightNavigationItem() {
        return mRightNavigationItem;
    }

    protected TextView getTitleItem() {
        return mTitleTextView;
    }

    @Override
    public void setTitle(CharSequence title) {
        super.setTitle(title);
        if (mTitleTextView != null) {
            mTitleTextView.setText(title);
        }
    }

    @Override
    public void setTitle(int titleId) {
        super.setTitle(titleId);
        if (mTitleTextView != null) {
            mTitleTextView.setText(titleId);
        }
    }

    private void handleChatBall() {
        if (shouldShowChatBall()) {
            ChatBallUtil.register(BaseActivity.this.getClass().getName());
        }
    }

    private void handleLockPattern() {
        if (!shouldProtectedByLockPattern()) {
            LockPatternHelper.registerIntoWhiteList(getClass().getName());
        }
    }

    protected Navigator getNavigator() {
        return NfsApplication.getApplication().getNavigator();
    }

    //If there is no need to show chat ball in the subclass, please override this function.
    protected boolean shouldShowChatBall() {
        return true;
    }

    protected boolean shouldProtectedByLockPattern() {
        return true;
    }

    /**
     * Get the Main Application component for dependency injection.
     *
     * @return {@link ApplicationComponent}
     */
    protected ApplicationComponent getApplicationComponent() {
        return ((NfsApplication) getApplication()).getApplicationComponent();
    }

    /**
     * Get an Activity module for dependency injection.
     *
     * @return {@link ActivityModule}
     */
    protected ActivityModule getActivityModule() {
        return new ActivityModule(this);
    }

}
